package github.kawaiior.yuanchuxiuzhen.core.init.event;

import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.gongfa.base.*;
import github.kawaiior.yuanchuxiuzhen.common.gongfa.pre.*;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.network.NetworkRegistryHandler;
import github.kawaiior.yuanchuxiuzhen.network.packet.PlayerLeftClickPacket;
import github.kawaiior.yuanchuxiuzhen.network.packet.UpdateManaPacket;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.network.PacketDistributor;

@Mod.EventBusSubscriber
public class GongFaEvent {

    @SubscribeEvent
    public static void onPlayerHarvestBlock(BlockEvent.BreakEvent event){
        if (event.isCanceled() || event.getWorld().isRemote()){
            return;
        }
        PlayerEntity player = event.getPlayer();
        World world = player.world;
        BlockState state = event.getState();
        Block block = state.getBlock();

        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            JinYaoJue.onPlayerDestroyBlock(player, world, block, power);
            JinGuangXuanGong.onPlayerDestroyBlock(player, world, block, power);

            QingMuJue.onPlayerDestroyBlock(player, world, block, power);
            BaiCaoZhenJing.onPlayerDestroyBlock(player, world, block, power);

            YanTuJue.onPlayerDestroyBlock(player, world ,block, power);
            QingShanZhenDiGong.onPlayerDestroyBlock(player, world, block, power);
        });
    }

    @SubscribeEvent(priority = EventPriority.LOWEST)
    public static void onPlayerAttack(LivingAttackEvent event){
        Entity attacker = event.getSource().getTrueSource();
        if (!(attacker instanceof PlayerEntity) || attacker.world.isRemote){
            return;
        }

        PlayerEntity player = (PlayerEntity) attacker;
        World world = player.world;

        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            if (player.isInWater()){
                XuanShuiJue.onPlayerAttack(player, world, event.getAmount(), power);
                YouShuiZhenJing.onPlayerAttack(player, world, event.getAmount(), power);
            }

            if (world.getDimensionKey() == World.THE_NETHER){
                ChiYanJue.onPlayerAttack(player, world, event.getAmount(), power);
                FenJinChiHuoJue.onPlayerAttack(player, world, event.getAmount(), power);
            }

            FenJinChiHuoJue.onPlayerAttackEvent(player, event, power);
        });
    }

    @SubscribeEvent
    public static void onPlayerHurt(LivingHurtEvent event){
        Entity hurtEntity = event.getEntityLiving();
        if (!(hurtEntity instanceof PlayerEntity) || hurtEntity.world.isRemote){
            return;
        }

        PlayerEntity player = (PlayerEntity) hurtEntity;
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{

        });
    }

    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event){
        if (event.phase != TickEvent.Phase.END || event.player.world.getGameTime() % 20 != 0){
            return;
        }

        PlayerEntity player = event.player;
        World world = player.world;
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power -> {
            JinGuangXuanGong.tick(player, world, power);
            YouShuiZhenJing.onPlayerTick(player, power);
            FenJinChiHuoJue.onPlayerTick(player, power);
            QingShanZhenDiGong.onPlayerTick(player, power);
        }));
    }

    @SubscribeEvent
    public static void onPlayerRender(RenderPlayerEvent event){
        PlayerEntity player = event.getPlayer();
        World world = player.world;

        JinGuangXuanGong.onPlayerRender(player, world, event);
    }

    // 还得在这里发个包，真麻烦
    @SubscribeEvent
    public static void onLeftClick(PlayerInteractEvent.LeftClickEmpty event) {
        PlayerEntity player = event.getPlayer();
        NetworkRegistryHandler.INSTANCE.sendToServer(new PlayerLeftClickPacket());
    }

}
